import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/home/index.vue";
import wx from 'weixin-js-sdk'
import store from "../store";
Vue.use(VueRouter);

// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
// 修改原型对象中的push方法
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};

const routes = [
  {
    // 首页
    path: "/",
    name: "home",
    component: Home,
    meta: {
      title: "数据学徒",
      keepAlive: true
    },
  },
  {
    // 首页
    path: "/home",
    name: "homePage",
    component: Home,
    meta: {
      title: "数据学徒",
      keepAlive: true
    },
  },
  {
    // 关于我们
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/about/index.vue"),
    meta: {
      title: "关于我们",
    },
  },
  {
    // 用户中心
    path: "/userCenter",
    name: "userCenter",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/userCenter/index.vue"),
    meta: {
      title: "用户中心",
      needLogin: true,
    },
  },
  {
    // 身份认证
    path: "/auditId",
    name: "auditId",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/userCenter/auditId.vue"),
    meta: {
      title: "身份认证",
      needLogin: true,
    },
  },
  {
    // 个人信息
    path: "/userInfo",
    name: "userInfo",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/userCenter/userInfo.vue"
      ),
    meta: {
      title: "个人信息",
      needLogin: true,
    },
  },
  {
    // 招聘专区
    path: "/hire",
    name: "hire",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/hire/index.vue"),
    meta: {
      title: "招聘专区",
      // needLogin: true,
      keepAlive: true
    },
  },
  {
    // 招聘专区- 职位列表
    path: "/jobList",
    name: "jobList",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(
        /* webpackChunkName: "jobList" */ "../views/companyAndJobList/index.vue"
      ),
    meta: {
      title: "职位列表",
      // needLogin: true,
    },
  },
  {
    // 招聘专区- 企业列表
    path: "/companyList",
    name: "companyList",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(
        /* webpackChunkName: "companyList" */ "../views/companyAndJobList/companyList.vue"
      ),
    meta: {
      title: "企业列表",
      // needLogin: true,
    },
  },
  {
    // 登录
    path: "/login",
    name: "login",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/login/index.vue"),
    meta: {
      title: "注册/登录",
    },
  },
  {
    // 学生风采专区
    path: "/studentStyle",
    name: "studentStyle",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/studentStyle/index.vue"),
    meta: {
      title: "学生风采",
    },
  },
  {
    // 新闻动态专区
    path: "/newsAction",
    name: "newsAction",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/newsAction/index.vue"),
    meta: {
      title: "前沿资讯",
    },
  },
  {
    // 新闻动态专区
    path: "/newsActionDetail",
    name: "newsActionDetail",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/newsActionDetail/index.vue"
      ),
    meta: {
      title: "前沿资讯详情",
    },
  },
  {
    // 投递记录页面
    path: "/deliverRecord",
    name: "deliverRecord",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/userCenter/deliverRecord.vue"
      ),
    meta: {
      title: "投递记录",
      needLogin: true,
    },
  },
  {
    // 面试邀约页面
    path: "/invited",
    name: "invited",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/userCenter/invited.vue"),
    meta: {
      title: "面试邀约",
      needLogin: true,
    },
  },
  {
    // 面试邀约页面
    path: "/invitedDetails",
    name: "invitedDetails",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/userCenter/invitedDetails.vue"),
    meta: {
      title: "面试邀约详情",
      needLogin: true,
    },
  },
  {
    // 拒绝面试邀约页面
    path: "/refuseInvited",
    name: "refuseInvited",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/userCenter/refuseInvited.vue"),
    meta: {
      title: "拒绝面试邀约",
      needLogin: true,
    },
  },
  {
    // 确认面试邀约页面
    path: "/confirmInvited",
    name: "confirmInvited",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/userCenter/confirmInvited.vue"),
    meta: {
      title: "确认面试邀约",
      needLogin: true,
    },
  },
  {
    // 学生风采详情
    path: "/studentStyleDetails",
    name: "studentStyleDetails",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/studentStyleDetails/index.vue"
      ),
    meta: {
      title: "学生风采详情",
    },
  },
  // 企业详情页面
  {
    path: "/company",
    name: "company",
    component: () => import("../views/company/index.vue"),
    meta: {
      title: "企业详情",
      needLogin: true,
    },
  },
  // 企业详情职位列表
  {
    path: "/companyJobList",
    name: "companyJobList",
    component: () => import("../views/company/companyJobList.vue"),
    meta: {
      title: "企业详情职位列表",
      needLogin: true,
    },
  },
  // 职位详情
  {
    path: "/jobDetail/:id",
    name: "jobDetail",
    component: () =>
      import(
        /* webpackChunkName: "jobDetail" */ "../views/jobDetail/index.vue"
      ),
    meta: {
      title: "职位详情",
      needLogin: true,
    },
  },
  // 用户信息
  {
    path: "/userInformation",
    name: "userInformation",
    component: () =>
      import(
        /* webpackChunkName: "userInformation" */ "../views/userInformation/index.vue"
      ),
    meta: {
      title: "用户信息",
      needLogin: true,
    },
  },
  // 账号设置（修改手机号） pc-homepage/src/views/userCenter/setAccount.vue
  {
    path: "/setAccount",
    name: "setAccount",
    component: () =>
      import(
        /* webpackChunkName: "setAccount" */ "../views/userCenter/setAccount.vue"
      ),
    meta: {
      title: "账号设置",
      needLogin: true,
    },
  },
  // 简历预览
  {
    path: "/resumeReview",
    name: "resumeReview",
    component: () =>
      import(
        /* webpackChunkName: "resumeReview" */ "../views/userCenter/resumeReview.vue"
      ),
    meta: {
      title: "查看简历",
      needLogin: true,
    },
  },
  // 编辑简历
  {
    path: "/resumeEdit",
    name: "resumeEdit",
    component: () =>
      import(
        /* webpackChunkName: "resumeEdit" */ "../views/userCenter/resumeEdit.vue"
      ),
    meta: {
      title: "编辑简历",
      needLogin: true,
      keepAlive: true
    },
  },
  {
    path: "/editItem",
    name: "editItem",
    component: () =>
      import(
        /* webpackChunkName: "resumeEdit" */ "../views/userCenter/editItem.vue"
      ),
    meta: {
      title: "编辑项简历",
      needLogin: true,
    },
  },
  // 应聘反馈
  {
    path: "/feedback",
    name: "feedback",
    component: () =>
      import(
        /* webpackChunkName: "feedback" */ "../views/userCenter/feedback.vue"
      ),
    meta: {
      title: "应聘反馈",
      needLogin: true,
    },
  },
  // 应聘结果反馈
  {
    path: "/feedbackDetails",
    name: "feedbackDetails",
    component: () =>
      import(
        /* webpackChunkName: "feedback" */ "../views/userCenter/feedbackDetails.vue"
      ),
    meta: {
      title: "应聘结果反馈",
      needLogin: true,
    },
  },
  // 其他服务
  {
    path: "/otherServices",
    name: "otherServices",
    component: () =>
      import(
        /* webpackChunkName: "feedback" */ "../views/otherServices/index.vue"
      ),
    meta: {
      title: "其他服务",
    },
  },
  // iframe页面，用于加载法律协议等html
  {
    path: "/iframePage",
    name: "iframePage",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/iframePage/index.vue"
      ),
    meta: {
      title: "数据学徒",
    },
  },
  {
    path: "/cultivate",
    name: "cultivate",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/cultivate/index.vue"
      ),
    meta: {
      title: "数据学徒",
    },
  },
  {
    path: "/inboxNotifications",
    name: "inboxNotifications",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/inboxNotifications/index.vue"
      ),
    meta: {
      title: "站内信",
    },
  },
  {
    path: "/inboxNotificationsDetails",
    name: "inboxNotificationsDetails",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/inboxNotificationsDetails/index.vue"
      ),
    meta: {
      title: "站内信信息内容",
    },
  },
  {
    path: "/addNotification",
    name: "addNotification",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/addNotification/index.vue"
      ),
    meta: {
      title: "新增留言",
    },
  },
  {
    path: "/researchResult",
    name: "researchResult",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/researchResult/index.vue"
      ),
    meta: {
      title: "研究成果",
    },
  },
  {
    path: "/resultList",
    name: "resultList",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/resultList/index.vue"
      ),
    meta: {
      title: "研究成果列表",
    },
  },
  {
    path: "/searchResultDetail/:key",
    name: "searchResultDetail",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/researchResult/detail.vue"
      ),
    meta: {
      title: "研究成果详情",
    },
  },
  {
    path: "/viewRecord",
    name: "viewRecord",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/userCenter/viewRecord.vue"
      ),
    meta: {
      title: "职位浏览记录",
    },
  },
  {
    path: "/sharePage",
    name: "sharePage",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/sharePage/index.vue"
      ),
  },
  {
    path: "/favorites",
    name: "favorites",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/favorites/index.vue"
      ),
    meta: {
      title: "收藏夹",
      needLogin: true,
    },
  },
  // 规划咨询-学生咨询
  {
    path: "/studentConsultation",
    name: "studentConsultation",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/consultation/student.vue"
      ),
    meta: {
      title: "学生咨询",
      needLogin: false,
    },
  },
  // companyConsultation 规划咨询-企业咨询
  {
    path: "/companyConsultation",
    name: "companyConsultation",
    component: () =>
      import(
        /* webpackChunkName: "iframePage" */ "../views/consultation/company.vue"
      ),
    meta: {
      title: "企业咨询",
      needLogin: false,
    },
  },
  {
    path: "/productManage",
    name: "productManage",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/productManage/index.vue"),
    meta: {
      title: "项目管理",
      needLogin: false,
      keepAlive: true
    },
  },
  {
    path: "/productList",
    name: "productList",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/productManage/productList.vue"),
    meta: {
      title: "项目列表",
      needLogin: false,
    },
  },
  {
    path: "/productDetail",
    name: "productDetail",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/productManage/productDetail.vue"),
    meta: {
      title: "项目详情",
      needLogin: true,
    },
  },
  {
    path: "/productJoin",
    name: "productJoin",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/productManage/productJoin.vue"),
    meta: {
      title: "项目报名",
      needLogin: true,
    },
  },
  {
    path: "/productResultList",
    name: "productResultList",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/productManage/productResultList.vue"),
    meta: {
      title: "项目结果公示",
      needLogin: true,
    },
  },
  {
    path: "/registrationProduct",
    name: "registrationProduct",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/userCenter/registrationProduct.vue"),
    meta: {
      title: "报名项目",
      needLogin: true,
    },
  },
  {
    path: "/checkProductDetail",
    name: "checkProductDetail",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/userCenter/checkProductDetail.vue"),
    meta: {
      title: "报名项目详情",
      needLogin: true,
    },
  },
  {
    path: "/editEntryProduct",
    name: "editEntryProduct",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/userCenter/editEntryProduct.vue"),
    meta: {
      title: "报名项目详情",
      needLogin: true,
    },
  },
  {
    path: "/matchManage",
    name: "matchManage",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/userCenter/matchManage.vue"),
    meta: {
      title: "比赛管理",
      needLogin: true,
    },
  },
  {
    path: "/productComment",
    name: "productComment",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/userCenter/productComment.vue"),
    meta: {
      title: "项目评论",
      needLogin: true,
    },
  },
  {
    path: "/productResultDetail",
    name: "productResultDetail",
    component: () => import(/* webpackChunkName: "iframePage" */ "../views/productManage/productResultDetail.vue"),
    meta: {
      title: "项目评论",
      needLogin: true,
    },
  },
];

//判断当前所在环境
export let testBrowser = () => {
  let ismo = false
  let ua = window.navigator.userAgent.toLowerCase()
  console.log(ua)
  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
    //ios的ua中无miniProgram，但都有MicroMessenger（表示是微信浏览器）
    wx.miniProgram.getEnv((res) => {
      console.log(res)
      if (res.miniprogram) {
        // 在小程序
        console.log('在小程序')
        ismo = true
      } else {
        //在微信里
        console.log('在微信里')
        ismo = false
      }
    })
  } else {
    console.log('不在微信里面')
    ismo = false

    // 不在微信里面
  }
  return ismo
}

const router = new VueRouter({
  mode: "hash",
  base: "/",
  routes,
});



router.beforeEach((to, from, next) => {
    // console.log(to, from, '路由跳转')
  if (!to.meta.keepAlive) {
    setTimeout(() => {
        // chrome
        document.body.scrollTop = 10
        document.body.scrollTop = 0
        // firefox
        document.documentElement.scrollTop = 10
        document.documentElement.scrollTop = 0
        // safari
        window.pageYOffset = 10
        window.pageYOffset = 0
    }, 20);
  }
  // 小程序登录的情况下
  if (to.query.Authorization) {
    localStorage.setItem("Authorization", to.query.Authorization)
    if (to.query.phone) {
      localStorage.setItem("userMobile", to.query.phone)
      // 设置store用户信息， 然后如果又跳转就跳转到需要跳转的地方
      // 手机号 跳转路由和参数{params,query}
      store.dispatch('FETCH_USER_INFO_WX', { mobile: to.query.phone, next, redirectUrl: to.query.redirectUrl || '', urlparams: to.query.urlparams || {} })
    } else {
      next()
    }
  } else {
    if (to.meta.needLogin) {
      if (localStorage.getItem("Authorization")) {
        next();
      } else {
        let isSmall = testBrowser()
        console.log(testBrowser(), '是否发送')
        if (isSmall) {
          //触发postmessage
          console.log('要发送了')
          wx.miniProgram.postMessage({
            data: { msg: '需要你登录了', isLogin: true }
          })
          let smallUrl = '/pages/login_home/index'
          // 如果跳转有参数
          if (to.path) {
            // 判断prams
            if (JSON.stringify(to.params) != '{}') {
              smallUrl += `?redirectUrl=${to.path}&params=${JSON.stringify(to.params)}`
            } else if (JSON.stringify(to.query) != '{}') {
              smallUrl += `?redirectUrl=${to.path}&query=${JSON.stringify(to.query)}`
            } else {
              smallUrl += `?redirectUrl=${to.path}`
            }
          }
          // wx.miniProgram.navigateBack({ delta: 1 })
          wx.miniProgram.navigateTo({
            url: smallUrl
          })
        } else {
          let redirectUrl = encodeURIComponent(window.location.href)
          next(`/login?redirectUrl=${redirectUrl}`, { replace: true });
        }
      }
    } else {
      next();
    }
  }
});
export default router;